package top.leoseven.adminproject.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import top.leoseven.adminproject.R;
import top.leoseven.adminproject.modle.StuHomeWorkInfo;
import top.leoseven.adminproject.view.StuHomeWorkInfoActivity;

/**
 * Created by admin2 on 2018/4/11.
 */

public class StuHomeWorkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private LayoutInflater inflater;
    private List<StuHomeWorkInfo> datas;

    public StuHomeWorkAdapter(Context context, List<StuHomeWorkInfo> datas) {
        this.context = context;
        this.datas = datas;
        this.inflater=LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = inflater.inflate(R.layout.item_homeworkinfo, parent, false);

        return new HomeWorkInfoHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HomeWorkInfoHolder) {
            final StuHomeWorkInfo info = datas.get(position);
            ((HomeWorkInfoHolder) holder).stuid.setText(info.getStudentid());
            ((HomeWorkInfoHolder) holder).resultnum.setText(info.getResultnum()+"");
            String s = info.getResult();
            if (s==null) {
                s="";
            }
            ((HomeWorkInfoHolder) holder).result.setText(s);
            String filepath = info.getFilepath();
            if (filepath==null) {
                filepath="";
            }
            ((HomeWorkInfoHolder) holder).filepath.setText(filepath);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent it = new Intent(context, StuHomeWorkInfoActivity.class);
                    it.putExtra("stuid",info.getStudentid());
                    if (info.getResult()==null) {
                        it.putExtra("result","");
                    }else{
                        it.putExtra("result",info.getResult());
                    }
                    if (info.getFilepath()==null){
                        it.putExtra("filepath","");
                    }else{
                        it.putExtra("filepath",info.getFilepath());
                    }
                    it.putExtra("workid",info.getId());
                    it.putExtra("resultnum",info.getResultnum());
                    context.startActivity(it);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }
    class HomeWorkInfoHolder extends RecyclerView.ViewHolder{
        private TextView stuid,result,resultnum,filepath;
        public HomeWorkInfoHolder(View itemView) {
            super(itemView);
            stuid= (TextView) itemView.findViewById(R.id.stuid);
            result= (TextView) itemView.findViewById(R.id.result);
            resultnum= (TextView) itemView.findViewById(R.id.resultnum);
            filepath= (TextView) itemView.findViewById(R.id.fileContent);
        }
    }
}
